package com.apes.crm.customerCar.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.crm.car.model.CarBase;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.Name;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.DateUtil;
import com.apes.scm.masterdata.area.model.Area;
import com.apes.scm.masterdata.car.model.CarConfiguration;
import com.apes.scm.masterdata.car.model.CarLabel;
import com.apes.scm.masterdata.car.model.CarModel;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.party.model.Party;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.party.repository.PartyRoleRepository;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.Date;


@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = CustomerCar.CONST_TABLE_NAME, indexes = {
        @Index(name = "IDX_CRM_CUSTOMER_CAR", columnList = "PARTY_ID,CAR_ID", unique = true),
        @Index(name = "IDX_CRM_CUSTOMER_PARTY", columnList = "PARTY_ID"),
        @Index(name = "IDX_CRM_CUSTOMER_CARID", columnList = "CAR_ID"),
        @Index(name = "IDX_CUSTOMER_CAR_PLATE_NUMBER", columnList = "PLATE_NUMBER")
})
public class CustomerCar extends Model {
    public static final String CONST_TABLE_NAME = "crm_customer_car";  // 客户车辆

    @Id
    @Column(name = "id", nullable = false, length = 24)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "客户车辆", padding = 8)
    private String id; // id

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "party_id", referencedColumnName = "id",nullable = false)
    private Party party ; //  客户信息

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "car_id",referencedColumnName = "id",nullable = false)
    private CarBase car; // 车辆id

    @Column(name = "contacts", length = 64)
    private String contacts; //联系人

    @Column(name = "contact_phone", length = 24)
    private String contactPhone;  //联系人电话

    @Name
    @Column(name = "plate_number", length = 64)
    private String plateNumber; //车牌号码

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "plate_type",referencedColumnName = "id")
    private Enumeration plateType; // 车牌类型 （0：临时牌；1：正式牌）

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "boarding_province_id", referencedColumnName = "id")
    private Area boardingProvince; // 上牌省份

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "boarding_city_id", referencedColumnName = "id")
    private Area boardingCity; // 上牌城市

    @Column(name = "drive_license_holder", length = 255)
    private String driveLicenseHolder; // 行驶证持有人

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "drive_license_reg_date")
    private Date driveLicenseRegDate; // 行驶证注册时间

    @Column(name = "drive_license_brand_model")
    private String driveLicenseBrandModel; // 行驶证品牌型号

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "drive_license_issue_date")
    private Date driveLicenseIssueDate; // 行驶证发证时间

    @Column(name = "drive_license_front_photo")
    private String driveLicenseFrontPhoto; // 行驶证正面

    @Column(name = "drive_license_reverse_photo")
    private String driveLicenseReversePhoto; // 行驶证反面

    @Column(name = "drive_license_side_photo")
    private String driveLicenseSidePhoto; // 行驶证附面

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "nature_of_use",referencedColumnName = "id")
    private Enumeration natureOfUse; // 使用性质 （1：营运；2：非营运 ）

    @Column(name = "authentication_mark",nullable = false,columnDefinition = "number(1,0) default 0")
    private boolean authenticationMark = false; // 认证标记 （0：非认证；1：认证 ）

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "authentication_date")
    private Date authenticationDate; // 认证时间

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "car_ownership",referencedColumnName = "id")
    private Enumeration carOwnership; // 车辆所有权 （0：单位；1：个人）

    @Column(name = "car_mortgage",nullable = false,columnDefinition = "number(1,0) default 0")
    private boolean carMortgage = false; // 抵押情况(0:否;1:是)

    @Column(name = "car_transfer",nullable = false,columnDefinition = "number(1,0) default 0")
    private boolean carTransfer = false; // 是否过户(0:否;1:是;)

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "car_transfer_date")
    private Date carTransferDate; // 过户日期

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "start_date")
    private Date startDate; // 有效期起

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "end_date")
    private Date endDate; // 有效期止

    @Column(name = "visits_count",nullable = false, columnDefinition = "number(4,0) default 0")
    private int visitsCount = 0; // 来店次数

    @Column(name = "total_spend_amt", columnDefinition = ApesConst.FIELD_QTY_16_4)
    private double totalSpendAmt; // 累计消费金额

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "last_visits_date")
    private Date lastVisitsDate; // 上次到店日期

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "car_status",referencedColumnName = "id")
    private Enumeration carStatus; // 车辆状态 （0：正常在用:1：寄售中:2：已报废）

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "car_frost_status" , referencedColumnName = "id")
    private Enumeration carFrostStatus; // 车辆冻结状态 (0:冻结;1:启用)


    /***
     * 功能：查询车型配置
     * @return
     */
    public CarConfiguration queryCarConfiguration() {
        if (car == null) return null;
        return car.getCarConfiguration();
    }

    /***
     * 功能：查询车型
     * @return
     */
    public CarModel queryCarModel() {
        if (car == null) return null;
        return car.queryCarModel();
    }

    /***
     * 功能：查询厂牌
     * @return
     */
    public CarLabel queryCarLabel() {
        if (car == null) return null;
        return car.queryCarLabel();
    }

    /***
     * 功能：查询客户
     * @return
     */
    public PartyRole queryCustomer() {
        if (party == null) return null;
        try {
            return SpringManager.getBean(PartyRoleRepository.class).findByPartyIdAndRoleId(party.getId(), "Customer");
        } catch (Exception e) {
            return null;
        }
    }

}
